<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="filePeriod">
        <el-date-picker v-model="queryParams.filePeriod" type="month" value-format="yyyy-MM" placeholder="选择账期"> </el-date-picker>
      </el-form-item>
      <el-form-item label="" prop="fileName">
        <el-input v-model="queryParams.fileName" placeholder="请输入文件名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="" prop="fileUpdateDate">
        <el-date-picker v-model="queryParams.fileUpdateDate" type="date" value-format="yyyy-MM-dd" placeholder="请输入文件日期"> </el-date-picker>
      </el-form-item>
      <el-form-item label="" prop="fileType">
        <el-select v-model="queryParams.fileType" placeholder="请选择文件类型" clearable style="width: 240px">
          <el-option label="账单" value="1" />
          <el-option label="起租单" value="0" />
          </el-select>
      </el-form-item>
      <el-form-item label="" prop="execStatus">
          <el-select v-model="queryParams.execStatus" placeholder="执行状态" clearable style="width: 240px">
            <el-option label="进行中" value="0" />
            <el-option label="完成" value="1" />
          </el-select>
        </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="taskList">
      <el-table-column label="账期" align="center" prop="filePeriod" />
      <el-table-column label="文件名" align="center" prop="fileName" />
      <el-table-column label="文件路径" align="center" prop="filePath" />
      <el-table-column label="执行状态" align="center" prop="execStatus">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.execStatus" active-value="1" inactive-value="0" @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="文件日期" align="center" prop="fileUpdateDate" />
      <el-table-column label="文件类型" align="center" prop="fileType" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="更新时间" align="center" prop="updateTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['monitor:task:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['monitor:task:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改任务列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="账期" prop="filePeriod">
          <el-input v-model="form.filePeriod" placeholder="请输入账期" />
        </el-form-item>
        <el-form-item label="文件名" prop="fileName">
          <el-input v-model="form.fileName" placeholder="请输入文件名" />
        </el-form-item>
        <el-form-item label="文件路径" prop="filePath">
          <el-input v-model="form.filePath" placeholder="请输入文件路径" />
        </el-form-item>
        <el-form-item label="文件日期" prop="fileUpdateDate">
          <el-input v-model="form.fileUpdateDate" placeholder="请输入文件日期" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTask, getTask, delTask, addTask, updateTask } from "@/api/monitor/task";

export default {
  name: "Task",
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务列表表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        filePeriod: null,
        fileName: null,
        filePath: null,
        execStatus: null,
        fileUpdateDate: null,
        fileType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /** 查询任务列表列表 */
    getList () {
      this.loading = true;
      listTask(this.queryParams).then(response => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        filePeriod: null,
        fileName: null,
        filePath: null,
        execStatus: null,
        fileUpdateDate: null,
        fileType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      let data = JSON.parse(JSON.stringify(row))
      this.open = true;
      this.form = data
      this.title = "修改任务列表";
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除任务列表编号为"' + ids + '"的数据项？').then(function () {
        return delTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    // 任务状态修改
    handleStatusChange (row) {
      let text = row.execStatus === "0" ? "启用" : "停用";
      let status = row.execStatus
      this.$modal.confirm('确认要"' + text + '""' + '"任务吗？').then(function () {
        return updateTask({ id: row.id, execStatus: status });
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
        this.getList()
      }).catch(function () {
        row.execStatus = row.execStatus === "0" ? "1" : "0";
      });
    },
  }
};
</script>
